package com.lc.pianostudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.ContactDetailBean;
import com.lc.pianostudent.bean.GetStudyPictureBean;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class ContactDetailAdapter extends RecyclerView.Adapter<ContactDetailAdapter.ViewHolder>{


    private Context context;

    private List<GetStudyPictureBean> list = new ArrayList<>();

    public ContactDetailAdapter(Context context, List<GetStudyPictureBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private ContactDetailAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(ContactDetailAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact_detail, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        ContactDetailAdapter.ViewHolder viewHolder = new ContactDetailAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {



        holder.day.setText(list.get(position).getDay());

        holder.month.setText(list.get(position).getMouse());

        if(!TextUtils.isEmpty(list.get(position).getPicarr())){
            holder.content.setVisibility(View.VISIBLE);
            holder.contact_img.setVisibility(View.VISIBLE);
            holder.content1.setVisibility(View.INVISIBLE);
            holder.content.setText(list.get(position).getContent());
            Glide.with(context).load(list.get(position).getPicarr()).into(holder.contact_img);
        }else {
            holder.content.setVisibility(View.INVISIBLE);
            holder.contact_img.setVisibility(View.INVISIBLE);
            holder.content1.setVisibility(View.VISIBLE);
            holder.content1.setText(list.get(position).getContent());
        }


        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final ContactDetailAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView content,day,month,content1;
        public ImageView contact_img;

        public ViewHolder(View itemView) {
            super(itemView);

            content = (TextView) itemView.findViewById(R.id.content);
            day = (TextView) itemView.findViewById(R.id.day);
            month = (TextView) itemView.findViewById(R.id.month);
            contact_img = itemView.findViewById(R.id.contact_img);
            content1 = (TextView) itemView.findViewById(R.id.content1);

        }
    }
}
